#include <bits/stdc++.h>
using namespace std;
#define int long long

int solve(int num) {
    int mins = 0;
    int maxs = 0;
    int hx1[10] = {};
    while(num > 0) {
        hx1[num%10]++;
        num /= 10;
    }
    for(int i = 0;i < 10;++ i) {
        for(int j = 0;j < hx1[i]; ++ j) {
            mins += i;
            mins *= 10;
        }
    }
    for(int i = 9;i >= 0;-- i) {
        for(int j = 0;j < hx1[i]; ++ j) {
            maxs += i;
            maxs *= 10;
        }
    }
    return maxs - mins;
}

signed main() {
    int num,k;
    cin >> num >> k;
    while(k--) {
        num = solve(num) / 10;
    }
    cout << num;
    return 0;
}
